package com.longxi.gamer_base.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by cxw on & 2018/8/28
 */
public class DateTimeUtils {
    private static String FORMAT_SEC_FULL = "yyyy-MM-dd HH:mm:ss";
    private static String FORMAT_YEAR_MONTH = "yyyy-MM";
    private static String FORMAT_YEAR_MONTH_DAY = "yyyy.MM.dd";
    /**
     * 时间转换成时间戳
     * @param time
     * @return
     */
    public static long dateToTimestamp(String time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_SEC_FULL);
        try {
            Date date = simpleDateFormat.parse(time);
//            long ts = date.getTime() / 1000;
            long ts = date.getTime();
            return ts;
        } catch (ParseException e) {
            return 0;
        }
    }

    /**
     * 时间戳转时间(11位时间戳)
     * @param time
     * @return
     */
    public static String timestampToDate(long time) {
        String dateTime;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_SEC_FULL);
        long timeLong = Long.valueOf(time);
        dateTime = simpleDateFormat.format(new Date(timeLong * 1000L));
        return dateTime;
    }

    public static String dateToYearMonth(Date time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_YEAR_MONTH);
        String date = simpleDateFormat.format(time);
        return date;
    }

    public static Date dateToYearMonth(String time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_YEAR_MONTH);
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String dateToYearMonthDay(Date time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_YEAR_MONTH_DAY);
        String date = simpleDateFormat.format(time);
        return date;
    }

    public static String dateToYearMonthDayHour(Date time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_SEC_FULL);
        String date = simpleDateFormat.format(time);
        return date;
    }

    public static String dateToYearMonthDayHour(String str){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_SEC_FULL);
        Date time = null;
        try {
            time = simpleDateFormat.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String date =dateToYearMonthDayHour(time);
        return date;
    }

    public static Date stringToData(String time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_SEC_FULL);
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}
